//作品类型切换
function swt(){
	$(function(){
		( $("input[name='type']:checked").val() == 1 ) ? swp() : swv();
		$("#type-video").click(function(){ swv(); });
		$("#type-photo").click(function(){ swp(); });
	});
}
function swv(){
		$('#content').remove();
		$('#UploadNote span').remove();
		$('<input name="content" id="content" class="inpt" />').appendTo('#TypeContent');
		$('<span><a href="http://darenshow.lovenest.cn/work/videoupload" target="_blank">如何上传视频？</a></span>').appendTo('#UploadNote');
		$("#VideoNote").show();
		$('#TypeContent strong').text('视频地址');
}
function swp(){
		$('#content').remove();
		$('#UploadNote span').remove();
		$('<input name="content" id="content" class="inpt" type="file" />').appendTo('#TypeContent');
		$('<span>图片5M以内，jpg或gif格式</span>').appendTo('#UploadNote');
		$("#VideoNote").hide();
		$('#TypeContent strong').text('上传图片');
}

//投票
function vote(wid){
	$.get("http://darenshow.lovenest.cn/vote/addvote/" + wid + "?rd=" + Math.round(Math.random()) , function(result){
		if (result < 0 ) {
			alert("请5分钟后再投票，谢谢合作！");
		}else{
			cont = $("#VoteCont");
			if(cont.length > 0){
				cont.text(result);
			}else{
				alert("投票成功！");
			}
		}
	});
}

//图片比例缩放
function resize(){
	$(function(){
		$("#PhotoShow img").each(function(){
			var width = 750;
			var image = $(this);
			if( image.width() > width ){
				image.width(width);
				image.height(width/image.width()*image.height());
			}
		});
	});
}

//滚动插件
(function($){
	$.fn.easyScroll = function(options){
		var defaults = {
			controlsShow:	true,
			prevId:			"prevBtn",
			prevText:		"",
			nextId:			"nextBtn",
			nextText:		"",
			speed:			500,
			pause:			3600,
			auto:			true,
			titleTip:		true,
			offset:			0
		}
		var options = $.extend(defaults,options);
		this.each(function(){
			var obj = $(this);
			var s = $("li", obj).length;
			var w = $("li", obj).outerWidth(true);
			
			//滚屏内容宽度
			$("ul", obj).css("width", w*s);
			
			//滚屏按钮
			if(options.controlsShow){
				var html = "";
				html += ' <span id="'+ options.prevId +'"><a href=\"javascript:void(0);\">'+ options.prevText +'</a></span>';
				html += ' <span id="'+ options.nextId +'"><a href=\"javascript:void(0);\">'+ options.nextText +'</a></span>';
				$(obj).append(html);
				function btnBind(){
					$("a","#"+options.nextId).click(function(){		
						animate("next",true);
					});
					$("a","#"+options.prevId).click(function(){		
						animate("prev",true);				
					});	
				}
				btnBind();
			}
			
			//滚屏动画
			function animate(dir,clicked){
				switch(dir){
					case "next"://前进
						$("ul", obj).animate({marginLeft:-options.offset},options.speed,function(){
							$(this).css({marginLeft:0}).find("li:first").appendTo(this);
						});
						break; 
					case "prev"://后退
						$("ul", obj).prepend($("ul", obj).css({marginLeft:-options.offset}).find("li:last"));
						$("ul", obj).animate({marginLeft:0},options.speed);
						break;
					default:
						break;
				};
				
				//取消自动滚屏
				if(clicked) clearTimeout(timeout);
				
				//自动滚屏
				if(!clicked){
					timeout = setTimeout(function(){
						animate("next",false);
					},options.pause);
				};
			}
			
			//提示
			if(options.titleTip){
				$("ul li", obj).each(function(){
					var t = $(this).find("img").attr("alt");
					$(this).find("img").attr("alt","");
					$(this).append("<div class='tip'>"+t+"</div>");
					$(this).find(".tip").hide();
					$(this).hover(
						function(){
							$(this).find(".tip").show();
						},
						function(){
							$(this).find(".tip").hide();
						}
					);
				});
			};
			
			//开始自动滚屏
			var timeout;
			if(options.auto){
				timeout = setTimeout(function(){
					animate("next",false);
				},options.pause);
			};
		});
	}
})(jQuery);